<template>
	<div class="layout-padding">
		<div class="layout-padding-auto layout-padding-view">
			<el-row v-show="showSearch">
				<el-form :model="state.queryForm" ref="queryRef" :inline="true" @keyup.enter="getDataList">
					<el-form-item :label="$t('Invitation.userInfoId')" prop="userInfoId">
						<el-input :placeholder="t('Invitation.inputUserInfoIdTip')" v-model="state.queryForm.userInfoId" style="max-width: 180px" />
					</el-form-item>
					<el-form-item :label="$t('Invitation.createUserName')" prop="createUserName">
						<el-input :placeholder="t('Invitation.inputCreateUserNameTip')" v-model="state.queryForm.createUserName" style="max-width: 180px" />
					</el-form-item>
					<el-form-item :label="$t('Invitation.createUserPhoto')" prop="createUserPhoto">
						<el-input :placeholder="t('Invitation.inputCreateUserPhotoTip')" v-model="state.queryForm.createUserPhoto" style="max-width: 180px" />
					</el-form-item>
					<el-form-item :label="$t('Invitation.invitationText')" prop="invitationText">
						<el-input :placeholder="t('Invitation.inputInvitationTextTip')" v-model="state.queryForm.invitationText" style="max-width: 180px" />
					</el-form-item>
					<el-form-item :label="$t('Invitation.invitationTitle')" prop="invitationTitle">
						<el-input :placeholder="t('Invitation.inputInvitationTitleTip')" v-model="state.queryForm.invitationTitle" style="max-width: 180px" />
					</el-form-item>
					<el-form-item :label="$t('Invitation.invitationType')" prop="invitationType">
						<el-input :placeholder="t('Invitation.inputInvitationTypeTip')" v-model="state.queryForm.invitationType" style="max-width: 180px" />
					</el-form-item>
					<el-form-item :label="$t('Invitation.isTop')" prop="isTop">
						<el-input :placeholder="t('Invitation.inputIsTopTip')" v-model="state.queryForm.isTop" style="max-width: 180px" />
					</el-form-item>
					<el-form-item :label="t('Invitation.topStartDate')" prop="topStartDate">
						<el-date-picker
							type="datetime"
							:placeholder="t('Invitation.inputTopStartDateTip')"
							v-model="state.queryForm.topStartDate"
						></el-date-picker>
					</el-form-item>
					<el-form-item :label="$t('Invitation.likesCount')" prop="likesCount">
						<el-input :placeholder="t('Invitation.inputLikesCountTip')" v-model="state.queryForm.likesCount" style="max-width: 180px" />
					</el-form-item>
					<el-form-item :label="$t('Invitation.collectCount')" prop="collectCount">
						<el-input :placeholder="t('Invitation.inputCollectCountTip')" v-model="state.queryForm.collectCount" style="max-width: 180px" />
					</el-form-item>
					<el-form-item :label="$t('Invitation.browseCount')" prop="browseCount">
						<el-input :placeholder="t('Invitation.inputBrowseCountTip')" v-model="state.queryForm.browseCount" style="max-width: 180px" />
					</el-form-item>
					<el-form-item :label="$t('Invitation.contactPhone')" prop="contactPhone">
						<el-input :placeholder="t('Invitation.inputContactPhoneTip')" v-model="state.queryForm.contactPhone" style="max-width: 180px" />
					</el-form-item>
					<el-form-item :label="$t('Invitation.state')" prop="state">
						<el-input :placeholder="t('Invitation.inputStateTip')" v-model="state.queryForm.state" style="max-width: 180px" />
					</el-form-item>
					<el-form-item :label="$t('Invitation.createDate')" prop="createDate">
						<el-input :placeholder="t('Invitation.inputCreateDateTip')" v-model="state.queryForm.createDate" style="max-width: 180px" />
					</el-form-item>
					<el-form-item :label="$t('Invitation.invitationLabel')" prop="invitationLabel">
						<el-input :placeholder="t('Invitation.inputInvitationLabelTip')" v-model="state.queryForm.invitationLabel" style="max-width: 180px" />
					</el-form-item>
					<el-form-item :label="$t('Invitation.releaseDate')" prop="releaseDate">
						<el-input :placeholder="t('Invitation.inputReleaseDateTip')" v-model="state.queryForm.releaseDate" style="max-width: 180px" />
					</el-form-item>
					<el-form-item>
						<el-button formDialogRef icon="search" type="primary" @click="getDataList">
							{{ $t('common.queryBtn') }}
						</el-button>
						<el-button icon="Refresh" formDialogRef @click="resetQuery">{{ $t('common.resetBtn') }}</el-button>
					</el-form-item>
				</el-form>
			</el-row>
			<el-row>
				<div class="mb8" style="width: 100%">
					<el-button
						formDialogRef
						icon="folder-add"
						type="primary"
						class="ml10"
						@click="formDialogRef.openDialog()"
						v-auth="'coalSystem_Invitation_add'"
					>
						{{ $t('common.addBtn') }}
					</el-button>
					<el-button
						plain
						formDialogRef
						:disabled="multiple"
						icon="Delete"
						type="primary"
						class="ml10"
						v-auth="'coalSystem_Invitation_del'"
						@click="handleDelete(selectObjs)"
					>
						{{ $t('common.delBtn') }}
					</el-button>
					<right-toolbar
						v-model:showSearch="showSearch"
						:export="'coalSystem_Invitation_export'"
						@exportExcel="exportExcel"
						class="ml10"
						style="float: right; margin-right: 20px"
						@queryTable="getDataList"
					></right-toolbar>
				</div>
			</el-row>
			<el-table
				:data="state.dataList"
				v-loading="state.loading"
				style="width: 100%"
				@selection-change="handleSelectionChange"
				@sort-change="sortChangeHandle"
			>
				<el-table-column type="selection" width="40" align="center" />
				<el-table-column type="index" :label="t('Invitation.index')" width="40" />
				<el-table-column prop="userInfoId" :label="t('Invitation.userInfoId')" sortable="custom" show-overflow-tooltip />
				<el-table-column prop="createUserName" :label="t('Invitation.createUserName')" show-overflow-tooltip />
				<el-table-column prop="createUserPhoto" :label="t('Invitation.createUserPhoto')" show-overflow-tooltip />
				<el-table-column prop="invitationText" :label="t('Invitation.invitationText')" show-overflow-tooltip />
				<el-table-column prop="invitationTitle" :label="t('Invitation.invitationTitle')" show-overflow-tooltip />
				<el-table-column prop="invitationType" :label="t('Invitation.invitationType')" sortable="custom" show-overflow-tooltip />
				<el-table-column prop="isTop" :label="t('Invitation.isTop')" sortable="custom" show-overflow-tooltip />
				<el-table-column prop="topStartDate" :label="t('Invitation.topStartDate')" sortable="custom" show-overflow-tooltip />
				<el-table-column prop="likesCount" :label="t('Invitation.likesCount')" show-overflow-tooltip />
				<el-table-column prop="collectCount" :label="t('Invitation.collectCount')" show-overflow-tooltip />
				<el-table-column prop="browseCount" :label="t('Invitation.browseCount')" show-overflow-tooltip />
				<el-table-column prop="contactPhone" :label="t('Invitation.contactPhone')" show-overflow-tooltip />
				<el-table-column prop="state" :label="t('Invitation.state')" sortable="custom" show-overflow-tooltip />
				<el-table-column prop="createDate" :label="t('Invitation.createDate')" sortable="custom" show-overflow-tooltip />
				<el-table-column prop="invitationLabel" :label="t('Invitation.invitationLabel')" sortable="custom" show-overflow-tooltip />
				<el-table-column prop="releaseDate" :label="t('Invitation.releaseDate')" sortable="custom" show-overflow-tooltip />
				<el-table-column :label="$t('common.action')" width="150">
					<template #default="scope">
						<el-button
							icon="edit-pen"
							text
							type="primary"
							v-auth="'coalSystem_Invitation_edit'"
							@click="formDialogRef.openDialog(scope.row.invitationId)"
							>{{ $t('common.editBtn') }}</el-button
						>
						<el-button icon="delete" text type="primary" v-auth="'coalSystem_Invitation_del'" @click="handleDelete([scope.row.invitationId])">{{
							$t('common.delBtn')
						}}</el-button>
					</template>
				</el-table-column>
			</el-table>
			<pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination" />
		</div>

		<!-- 编辑、新增  -->
		<form-dialog ref="formDialogRef" @refresh="getDataList(false)" />
	</div>
</template>

<script setup lang="ts" name="systemCoalInvitation">
import { BasicTableProps, useTable } from '/@/hooks/table';
import { fetchList, delObjs } from '/@/api/coalSystem/Invitation';
import { useMessage, useMessageBox } from '/@/hooks/message';
import { useDict } from '/@/hooks/dict';
import { useI18n } from 'vue-i18n';

// 引入组件
const FormDialog = defineAsyncComponent(() => import('./form.vue'));
const { t } = useI18n();
// 定义查询字典

// 定义变量内容
const formDialogRef = ref();
// 搜索变量
const queryRef = ref();
const showSearch = ref(true);
// 多选变量
const selectObjs = ref([]) as any;
const multiple = ref(true);

const state: BasicTableProps = reactive<BasicTableProps>({
	queryForm: {},
	pageList: fetchList,
});

//  table hook
const { getDataList, currentChangeHandle, sizeChangeHandle, sortChangeHandle, downBlobFile } = useTable(state);

// 清空搜索条件
const resetQuery = () => {
	// 清空搜索条件
	queryRef.value?.resetFields();
	// 清空多选
	selectObjs.value = [];
	getDataList();
};

// 导出excel
const exportExcel = () => {
	downBlobFile('/coalSystem/Invitation/export', state.queryForm, 'Invitation.xlsx');
};

// 多选事件
const handleSelectionChange = (objs: any) => {
	selectObjs.value.push(...objs.map((val: any) => val.invitationId));
	multiple.value = !objs.length;
};

// 删除操作
const handleDelete = async (ids: string[]) => {
	try {
		await useMessageBox().confirm(t('common.delConfirmText'));
	} catch {
		return;
	}

	try {
		await delObj(ids);
		getDataList();
		useMessage().success(t('common.delSuccessText'));
	} catch (err: any) {
		useMessage().error(err.msg);
	}
};
</script>
